package com.zbkj.admin.controller;

import com.zbkj.admin.filter.TokenComponent;
import com.zbkj.admin.service.AdminEstimateAiHandleService;
import com.zbkj.admin.service.AdminProjectHandleV2Service;
import com.zbkj.common.model.project.EstimateAi;
import com.zbkj.common.model.system.SystemAdmin;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.enquiry.ReportRequest;
import com.zbkj.common.request.project.*;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.response.UploadResponse;
import com.zbkj.common.response.project.*;
import com.zbkj.common.response.standard.ReportDetailResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther:
 * @Date: 2024/9/13 08:55
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("api/admin/project")
@Api(tags = "服务端项目管理服务V2")
public class AdminProjectV2Controller {

    @Autowired
    private AdminProjectHandleV2Service adminProjectHandleV2Service;
    @Autowired
    private AdminEstimateAiHandleService adminEstimateAiHandleService;
    @Resource
    private TokenComponent tokenComponent;

    @ApiOperation(value="项目列表")
    @PostMapping("/list")
    public CommonResult<CommonPage<AdminProjectInfoResponse>> list(@RequestBody AdminProjectQueryRequest queryRequest) {
        return CommonResult.success(CommonPage.restPage(adminProjectHandleV2Service.projectList(queryRequest)));
    }

    @ApiOperation(value="获取项目信息")
    @GetMapping("/getProjectInfo/{projectId}")
    public CommonResult<AdminProjectInfoResponse> getProjectInfo(@PathVariable("projectId") Long projectId) {
        AdminProjectInfoResponse response = adminProjectHandleV2Service.getProjectInfo(projectId);
        return CommonResult.success(response);
    }

    @ApiOperation(value="获取项目效源文件列表")
    @GetMapping("/getValidProjectFile/{projectId}")
    public CommonResult<List<InformationResponse>> getValidProjectFileList(@PathVariable("projectId") Long projectId) {
        return CommonResult.success(adminProjectHandleV2Service.getValidProjectFileList(projectId));
    }

    @ApiOperation(value="拆分需求内容提交")
    @PostMapping("/aiContentSubmit")
    public CommonResult aiContentSubmit(@RequestBody @Validated AdminAiContentRequest contentRequest, HttpServletRequest request) {
        SystemAdmin systemAdmin =  tokenComponent.getLoginUser(request).getUser();
        adminProjectHandleV2Service.aiContentSubmit(contentRequest,systemAdmin);
        return CommonResult.success();
    }

    @ApiOperation(value="获取需求拆分内容")
    @GetMapping("/aiContentList/{projectId}")
    public CommonResult<List<EstimateAi>> aiContentList(@PathVariable("projectId") Long projectId) {
        return CommonResult.success(adminProjectHandleV2Service.aiContentList(projectId));
    }

    @ApiOperation(value="查看估算结果")
    @GetMapping("/getEstimateResult/{projectId}")
    public CommonResult<EstimateRestProjectResponse> getEstimateResult(@PathVariable("projectId") Long projectId) {
        return CommonResult.success(adminProjectHandleV2Service.getEstimateResult(projectId));
    }

    @ApiOperation(value="编辑估算功能点明细保存")
    @PostMapping("/estimateFunSave")
    public CommonResult<EstimateFunEditRestResponse> estimateFunSave(@RequestBody @Validated AdminEstimateFunSaveRequest estimateFunSaveRequest, HttpServletRequest request) {
        SystemAdmin systemAdmin = tokenComponent.getLoginUser(request).getUser();
        return CommonResult.success(adminProjectHandleV2Service.estimateFunSave(estimateFunSaveRequest,systemAdmin));
    }

    @ApiOperation(value="下载编辑后估算功能点明细文件")
    @GetMapping("/getEstimateFunFile/{projectId}")
    public CommonResult<InformationResponse> getValidInformation(@PathVariable("projectId") Long projectId, HttpServletRequest request) {
        SystemAdmin systemAdmin = tokenComponent.getLoginUser(request).getUser();
        return CommonResult.success(adminProjectHandleV2Service.getEstimateFunFile(projectId,systemAdmin));
    }

    @ApiOperation(value="项目完成操作(复审完成|报告完成)")
    @GetMapping("/statusConfirm/{projectId}/{status}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId",value = "项目ID"),
            @ApiImplicitParam(name = "status",value = "处理类型：30 复审完成,50 报告完成")
    })
    public CommonResult statusConfirm(@PathVariable("projectId") Long projectId, @PathVariable("status") Integer status) {
        adminProjectHandleV2Service.statusConfirm(projectId, status);
        return CommonResult.success();
    }

    @ApiOperation(value="报告申请审核")
    @PostMapping("/reportApplyAudit")
    public CommonResult reportApplyAudit(@RequestBody @Validated AdminReportApplyAuditRequest auditRequest, HttpServletRequest request) {
        /*SystemAdmin systemAdmin = tokenComponent.getLoginUser(request).getUser();
        adminProjectHandleV2Service.reportApplyAudit(auditRequest, systemAdmin);*/
        return CommonResult.success();
    }

    @ApiOperation(value="服务端-项目资料上传(报告文件)")
    @PostMapping("/informationUpload/{type}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "材料类型：43、报告文件"),
    })
    public CommonResult<UploadResponse> informationUpload(@PathVariable("type")Integer type, MultipartFile file) {
        return CommonResult.success(adminProjectHandleV2Service.informationUpload(type,file));
    }

    @ApiOperation(value="报告通知客户端")
    @GetMapping("/reportNotifClient/{projectId}")
    public CommonResult reportNotifClient(@PathVariable("projectId") Long projectId) {
        adminProjectHandleV2Service.reportNotifClient(projectId);
        return CommonResult.success();
    }

    @ApiOperation(value="服务端-估算文件保存(报告文件)")
    @PostMapping("/estimateFileSave")
    public CommonResult estimateFileSave(@RequestBody @Validated AdminInfomationRequest infomationRequest, HttpServletRequest request) {
        SystemAdmin systemAdmin = tokenComponent.getLoginUser(request).getUser();
        adminProjectHandleV2Service.estimateFileSave(infomationRequest,systemAdmin);
        return CommonResult.success();
    }

    @ApiOperation(value="获取有效资料对象-下载资料")
    @GetMapping("/getValidEstimateFile/{projectId}/{type}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "材料类型：41、ai估算结果文件，43、报告文件"),
    })
    public CommonResult<InformationResponse> getValidInformation(@PathVariable("projectId") Long projectId,@PathVariable("type") Integer type) {
        return CommonResult.success(adminProjectHandleV2Service.getValidEstimateFile(projectId, type));
    }

    @ApiOperation(value="估算结果AI回调通知")
    @PostMapping("/estimateAiNotify")
    public CommonResult estimateAiNotify(@RequestBody @Validated AiNotifyRequest notifyRequest, HttpServletRequest request) {
        adminEstimateAiHandleService.estimateAiNotify(notifyRequest,request);
        return CommonResult.success();
    }

    @ApiOperation(value="项目调整因子编辑保存")
    @PostMapping("/projectParamEditSave")
    public CommonResult projectParamEditSave(@RequestBody @Validated ProjectParamSaveRequest paramSaveRequest) {
        adminProjectHandleV2Service.projectParamEditSave(paramSaveRequest);
        return CommonResult.success();
    }

    @ApiOperation(value="获取估算明细索引文档")
    @GetMapping("/getEstimateFunDoc/{projectId}")
    public CommonResult<List<EstimateFunDocResponse>> getEstimateFunDoc(@PathVariable("projectId") Long projectId) {
        return CommonResult.success(adminProjectHandleV2Service.getEstimateFunDoc(projectId));
    }

    @ApiOperation(value="估算功能明细推送ai")
    @GetMapping("/estimateFunToAi/{projectId}")
    public CommonResult estimateFunToAi(@PathVariable("projectId") Long projectId) {
        adminProjectHandleV2Service.estimateFunToAi(projectId);
        return CommonResult.success();
    }

    @ApiOperation(value="提交邮寄信息")
    @PostMapping("/commitDelivery/{projectId}")
    public CommonResult submitDelivery(@PathVariable("projectId") Long projectId,@RequestBody ProjectDeliveryRequest queryRequest) {
        if (!adminProjectHandleV2Service.submitDelivery(projectId,queryRequest)) {
            return CommonResult.failed();
        }
        return CommonResult.success();
    }

    @ApiOperation(value="AI统计")
    @PostMapping("/aiReport")
    public CommonResult<AiReportResponse> aiReport(@RequestBody ReportRequest reportRequest) {
        return CommonResult.success(adminProjectHandleV2Service.aiReport(reportRequest));
    }

    @ApiOperation(value="测算明细")
    @PostMapping("/estimateDetail/{projectId}")
    public CommonResult<List<ReportDetailResponse>> estimateDetail(@PathVariable("projectId") Long projectId) {
        return CommonResult.success(adminProjectHandleV2Service.estimateDetail(projectId));
    }

    @ApiOperation(value="文件上传")
    @PostMapping("/fileUpload")
    public CommonResult<UploadResponse> fileUpload(MultipartFile file) {
        return CommonResult.success(adminProjectHandleV2Service.fileUpload(file));
    }

    @ApiOperation(value="项目类别统计")
    @GetMapping("/projectTypeStatistics/{projectId}")
    public CommonResult<List<ProjectTypeStatisticsResponse>> projectTypeStatistics(@PathVariable("projectId") Long projectId) {
        return CommonResult.success(adminProjectHandleV2Service.projectTypeStatistics(projectId));
    }
}
